package cn.wolfcode.wolf2w.conteoller;

import cn.wolfcode.wolf2w.annontation.RequireLogin;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IStrategyRedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserInfoController {
    @Autowired
    private IUserInfoService userInfoService ;

    @Autowired
    private IUserInfoRedisService userInfoRedisService ;

    @Autowired
    private IStrategyRedisService strategyRedisService ;
    @GetMapping("/get")
    public Object get(String id){
        return userInfoService.get(id);
    }

    @GetMapping("/checkPhone")
    public Boolean checkPhone(String phone){
        Boolean judge = userInfoService.checkPhone(phone);
        return judge ;
    }

    @GetMapping("/sendVerifyCode")
    public JsonResult sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        return JsonResult.success();
    }

    @GetMapping("/checkNickname")
    public JsonResult checkNickname(String nickname){
        System.out.println("开启验证");
        UserInfo userInfo = userInfoService.checkNickname(nickname);
        if(userInfo!=null){
            return new JsonResult(444,"用户名已存在,请重新输入",null);
        }
        return new JsonResult(666,"验证成功",null);
    }

    @GetMapping("/setPassword")
    public JsonResult setPassword(String nickname ,String password){
        userInfoService.setPassword(nickname,password);
        return JsonResult.success();
    }
    @GetMapping("/checkPassword")
    public Boolean checkPassword(String nickname,String rpassword){
        Boolean reft = userInfoService.checkPassword(nickname,rpassword);
        return reft;
    }
    @PostMapping("/regist")
    public JsonResult regist(String phone ,String password,String nickname,String rpassword,String verifyCode){
        userInfoService.regist(phone,password,nickname,rpassword,rpassword,verifyCode);
        return JsonResult.success();
    }

    @PostMapping("/login")
    public JsonResult login(String username ,String password){
        UserInfo user = userInfoService.login(username,password);
        String token = userInfoRedisService.saveToken(user);
        Map<String,Object> map = new HashMap<>();
        map.put("user",user );
        map.put("token",token );
        return JsonResult.success(map);
    }
    @RequireLogin
    @GetMapping("/currentUser")
    public JsonResult currentUser(HttpServletRequest req) {
        System.out.println(1);
        String token = req.getHeader("token");
        UserInfo user = userInfoRedisService.getUserByToken(token);
        return JsonResult.success(user);
    }

    @GetMapping("/strategies/favor")
    public JsonResult getSids(String userId) {
        List<String> sids = strategyRedisService.getSids(userId);
        return JsonResult.success(sids);
    }
}
